//
//  FQDFontSettingCell.m
//  FakeQDaily
//
//  Created by mewxiv on 9/9/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDFontSettingCell.h"
#import "FQDSettingOption.h"

@interface FQDFontSettingCell ()

// 字体大小按钮列表
@property (nonatomic, strong) NSMutableArray *fontButtons;
@property (nonatomic, strong) NSArray *unselectFontImages;
@property (nonatomic, strong) NSArray *selectedFontImages;

@end

@implementation FQDFontSettingCell
@synthesize option = _option;

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self prepareSettingUI];
    }
    return self;
}

- (void)prepareSettingUI
{
    
    CGRect optionViewRect = CGRectMake(0, 0, 140.0f, 45.0f);
    UIView *fontOptionView = [[UIView alloc] initWithFrame:optionViewRect];
    fontOptionView.backgroundColor = [UIColor clearColor];
    self.accessoryView = fontOptionView;
    
    CGFloat x = 0, y = 1.5f;
    CGFloat offsetX = 50.0f;
    self.fontButtons = [NSMutableArray arrayWithCapacity:3];
    for (NSInteger i = 0; i < 3; i++) {
        NSArray *imageNameArray = (_sizeCategory == i) ? self.selectedFontImages : self.unselectFontImages;
        NSString *buttonImageName = imageNameArray[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(x, y, 40.0f, 40.0f);
        [button setImage:[UIImage imageNamed:buttonImageName] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(fontButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [fontOptionView addSubview:button];
        [self.fontButtons addObject:button];
        
        x += offsetX;
    }
}

- (void)fontButtonTapped:(UIButton *)selectedCategoryBtn
{
    NSInteger selectedCategory = [self.fontButtons indexOfObject:selectedCategoryBtn];
    if (self.sizeCategory != selectedCategory) {
        self.sizeCategory = selectedCategory;
        
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults setObject:@(selectedCategory) forKey:kQDailyFontSizeCategoryUserDefaultsKey];
        
        NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
        [defaultCenter postNotificationName:FQDFontSizeCategoryDidChangeNotification
                                     object:nil];
    }
}

- (NSArray *)unselectFontImages
{
    if (!_unselectFontImages) {
        _unselectFontImages = @[ @"little_select", @"middle_select", @"big_select" ];
    }
    return _unselectFontImages;
}

- (NSArray *)selectedFontImages
{
    if (!_selectedFontImages) {
        _selectedFontImages = @[ @"little_selected", @"middle_selected", @"big_selected" ];
    }
    return _selectedFontImages;
}

- (void)setOption:(FQDSettingOption *)option
{
    _option = option;
    self.textLabel.text = option.title;
    self.sizeCategory = [option.value integerValue];
}

- (void)setSizeCategory:(FQDFontSizeCategory)sizeCategory
{
    UIButton *preSelectedCategoryBtn = [self.fontButtons objectAtIndex:_sizeCategory];
    UIButton *selectedCategoryBtn = [self.fontButtons objectAtIndex:sizeCategory];
    
    NSString *unselectImageName = self.unselectFontImages[_sizeCategory];
    [preSelectedCategoryBtn setImage:[UIImage imageNamed:unselectImageName] forState:UIControlStateNormal];
    
    NSString *selectedImageName = self.selectedFontImages[sizeCategory];
    [selectedCategoryBtn setImage:[UIImage imageNamed:selectedImageName] forState:UIControlStateNormal];
    
    _sizeCategory = sizeCategory;
}

+ (NSString *)reuseId
{
    return @"FQDFontSettingCell";
}

@end
